package com.jy.api.controller.pos;

import com.jy.api.dao.InventoryDao;
import com.jy.api.dao.ProductDao;
import com.jy.api.domain.Inventory;
import com.jy.api.domain.Product;
import com.jy.api.service.ProductService;
import com.jy.api.service.ProductTypeService;
import com.jy.api.vo.pos.productInVo.ProductInVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2018/1/31.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/pos", produces = "application/json;charset=utf-8")
public class PosStockController {


    @Autowired
    ProductService productService;


    @Autowired
    ProductDao productDao;

    @Autowired
    ProductTypeService typeService;

    @Autowired
    private InventoryDao inventoryDao;

    /**
     * 库存首页数据获取
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/inventory")
    public ResponseEntity homePage(@PathVariable String storeId,
                                   @RequestParam(value = "typeId", defaultValue = "") String typeId,
                                   @PageableDefault(value = 10, sort = {"quantity"}, direction = Sort.Direction.DESC) Pageable pageable) {
        try {
            List<String> typeIds = new ArrayList<>();
            if (StringUtils.isNotBlank(typeId)) {
                typeIds = typeService.getTypeIds(typeId);
            }

            return new ResponseEntity(productService.getStockHomeInfo(storeId, typeIds, pageable, false), HttpStatus.OK);
        } catch (Exception ex) {

            log.error("homePage is error==> storeId{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 首页数据获取
     * @param storeId
     * @param pageable
     * @return
     */
    @GetMapping("get-InP")
    public ResponseEntity getProductIn(@RequestParam("storeId") String storeId, Pageable pageable) {
        return new ResponseEntity(productService.findHomePageProduct(storeId, pageable), HttpStatus.OK);
    }

    /**
     * 首页数据获取
     * @param storeId
     * @param pageable
     * @return
     */
    @GetMapping("get-material-inP")
    public ResponseEntity getMaterialIn(@RequestParam("storeId") String storeId, Pageable pageable) {
        return new ResponseEntity(productService.findHomePageMaterial(storeId, pageable), HttpStatus.OK);
    }


    /**
     * 单个商品库存信息获取
     *
     * @param storeId
     * @param productId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/stock/product-search")
    public ResponseEntity homePage(@PathVariable String storeId,
                                   @RequestParam(value = "productId") String productId) {
        try {
            Product product = productDao.findOne(productId);
            if (product == null) {
                return new ResponseEntity("商品信息不存在！", HttpStatus.BAD_REQUEST);
            }
            if (!product.isStockEnabled()) {
                return new ResponseEntity("商品未管理库存！！", HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity(productService.getProductInfo(product), HttpStatus.OK);
        } catch (Exception ex) {

            log.error("homePage is error==> storeId{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
